/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.util.review.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

/**
 * Checks if the given XML file is valid with an XML schema.
 * 
 * @author Dimitrij Pankratz, Anton Musichin
 * @version 1.02
 */
public class XMLSchemaValidator implements Serializable {
	private static final long serialVersionUID = -8156164640588498965L;

	private Validator validator = null;

	private String lastErrorMsg;
	
	/**
	 * Class constructor.
	 * 
	 * Default XML schema will be used.
	 * @throws SAXException
	 */
	public XMLSchemaValidator() throws SAXException {
		this(null);
	}
	
	/**
	 * Class constructor.
	 * 
	 * @param schemaFile XML schema file. 
	 * If schemaFile is null, default XML schema will be used.
	 * @throws SAXException
	 */
	public XMLSchemaValidator(URL schemaFile) throws SAXException {
		SchemaFactory schemaFac = 
				SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		if(schemaFile == null)
			schemaFile = getClass().getResource("schema.xsd");
		Schema schema = schemaFac.newSchema(schemaFile);
		validator = schema.newValidator();
	}
	
	/**
	 * Returns true if the file is valid, otherwise false.
	 * 
	 * @param file File to check.
	 * @return True if file is valid, otherwise false.
	 * @throws IOException
	 */
	public boolean validate(File file) throws IOException {
		return validate(new StreamSource(file));
	}
	
	/**
	 * Returns true if the input is valid, otherwise false.
	 * 
	 * @param file File to check.
	 * @return True if file is valid, otherwise false.
	 * @throws IOException
	 */
	public boolean validate(InputStream stream) throws IOException {
		return validate(new StreamSource(stream));
	}
	
	/**
	 * Returns true if the source is valid, otherwise false.
	 * 
	 * @param stream File to check.
	 * @return True if file is valid, otherwise false.
	 * @throws IOException
	 */
	public boolean validate(Source source) throws IOException {
		boolean result = true;
		try {
			validator.validate(source);
		} catch (SAXException e) {
			this.lastErrorMsg = e.getMessage();
			result = false;
		}
		return result;
	}
	
	public String getLastErrorMessage() {
		return lastErrorMsg;
	}
}
